home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 45975 / 45975.xpi / content / options.js < prev    next >
Text File  |  2009-11-23  |  7KB  |  171 lines

  1. /*
  2.  * Copyright (c) 2009 Bui Viet Thanh (thanhbv@gmail.com).
  3.  *
  4.  * This file is part of clicknlearn.
  5.  *
  6.  * clicknlearn is free software: you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation, either version 3 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * clicknlearn is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with clicknlearn.  If not, see <http://www.gnu.org/licenses/>.
  18.  */
  19.  
  20. Components.utils.import("resource://clicknlearn/ext/Observers.js");
  21. Components.utils.import("resource://clicknlearn/cnlprefs.js");
  22.  
  23. var CNL_Options = {
  24.     /**
  25.      * currentDicIndex is currentRowIndex - 1 
  26.      */
  27.     currentRowIndex: -1,
  28.     rows: null,
  29.     /**
  30.      * Get indexOf elem in elem.parentNode.childNodes
  31.      */
  32.     _getChildIndex: function(elem){
  33.         var e, k=0;
  34.         for (e=elem; e = e.previousSibling; ++k){};
  35.         return k;        
  36.     },
  37.     eventLoadImpl: function(event){
  38.         this.rows = document.getElementById("dics-rows");
  39.         this.constructXul();
  40.         addEventListener("focus", function(event){CNL_Options.eventFocusImpl(event);}, true);
  41.     },
  42.     eventFocusImpl: function(event){
  43.         var parent = event.target.parentNode;
  44.         if(!parent || parent.parentNode != this.rows)
  45.             return;
  46.         var newRowIndex = this._getChildIndex(parent);
  47.         this._changeFocusedRow(newRowIndex);
  48.     },
  49.     _changeFocusedRow: function(newRowIndex){
  50.         if(this.currentRowIndex == newRowIndex)
  51.             return;
  52.         this.rows.childNodes[newRowIndex].style.backgroundColor = "ThreeDShadow";
  53.         if(this.currentRowIndex != -1)
  54.             this.rows.childNodes[this.currentRowIndex].style.backgroundColor = "-moz-Dialog";
  55.         if(this.currentRowIndex == 1)//change focus from first dic
  56.             document.getElementById("buttonMoveUp").setAttribute("disabled", "false");
  57.         if(newRowIndex == 1)//change focus to first dic
  58.             document.getElementById("buttonMoveUp").setAttribute("disabled", "true");
  59.         this.currentRowIndex = newRowIndex;  
  60.     },
  61.     /**
  62.      * construct options.xul dynamicly: add rows.
  63.      */
  64.     constructXul: function(){
  65.         var dicts = CNL_Prefs.getDicsArrayPref();
  66.         if(dicts.length == 0)
  67.             dicts =
  68.             [['true', 'tratu.vn', 'http://tratu.vn/dispatchaddon.php?dict=en_vn&title=',
  69.                 '<i>Bß║ín ─æang tra tß╗½', '']
  70.             ,['true', 'vdict.com', 'http://vdict.com/fsearch.php?dictionaries=eng2vie_vie2eng_foldoc&word=',
  71.                 ' <!-- Database --><!-- Database --><!-- Database -->Not Found.', '']
  72.             ,['true', 'edictx.com', 'http://edictx.com/edictx/?dict=1&mode=addon&word=',
  73.                 ' was not found!<', '']
  74.             ,['true', 'tudientiengviet.net', 'http://www.tudientiengviet.net/get.php?mode=quickdict&dict=en-vi&word=',
  75.                 'C├íc tß╗½ t╞░╞íng tß╗▒: <br><ul>', '']];
  76.         for(var i in dicts)
  77.             this._showDictionary(dicts[i]);
  78.     },
  79.     _showDictionary: function(dic){
  80.         var j, row, xbox, //checkbox or textbox
  81.             //each row will contain 5 fields:
  82.             //[".enable", ".name", ".baseUrl", ".notfoundPattern", ".selectors"],
  83.             textboxesSize = ["13", "60", "35", "15"];
  84.         row = document.createElement("row");
  85.  
  86.         xbox = document.createElement("checkbox");
  87.         xbox.setAttribute("checked", dic[0] == "true");
  88.         row.appendChild(xbox);
  89.         for(j=1; j<5; j++){
  90.             xbox = document.createElement("textbox");
  91.             xbox.setAttribute("size", textboxesSize[j-1]);
  92.             xbox.setAttribute("value", dic[j]);
  93.             row.appendChild(xbox);
  94.         }
  95.         this.rows.appendChild(row);
  96.     },
  97.     addDictionary: function(){
  98.         this._showDictionary(["true", "", "", "", ""]);
  99.         this._changeFocusedRow(this.rows.childElementCount - 1);
  100.     },
  101.     /**
  102.      * remove currentDic row & the corresponds preferences
  103.      */
  104.     removeDictionary: function(){
  105. //        alert(this.currentRowIndex);
  106. //        if(this.currentRowIndex <= 0)
  107. //            return;
  108.         if(this.rows.childElementCount == 2){
  109.             alert("Can't remove all dictionary sources!");
  110.             return;
  111.         }
  112.         var oldChild = this.rows.childNodes[this.currentRowIndex];
  113.         if(this.currentRowIndex == this.rows.childElementCount - 1)
  114.             this._changeFocusedRow(this.currentRowIndex - 1);
  115.         else
  116.             this._changeFocusedRow(this.currentRowIndex + 1);
  117.         this.rows.removeChild(oldChild);
  118.     },
  119.     /**
  120.      * moveUp current dictionary (current row)
  121.      * condition: current dic must not be first dic
  122.      * (we ensure this by disabling "Move Up" button if first dic got focus)
  123.      */
  124.     moveUpDictionary: function(){
  125.         var currRow = this.rows.removeChild(this.rows.childNodes[this.currentRowIndex]);
  126.         this.rows.insertBefore(currRow, this.rows.childNodes[this.currentRowIndex - 1]);
  127.         this._changeFocusedRow(this.currentRowIndex - 1);
  128.     },
  129.     acceptDialog: function(){
  130.         var dicservices = [];
  131.         for(var i=1; i<this.rows.childElementCount; i++){
  132.             var row  = this.rows.childNodes[i];
  133.             var dic = [row.childNodes[0].checked];
  134.             for(j=1; j<5; j++)
  135.                 dic.push(row.childNodes[j].value);
  136.             dicservices.push(dic);
  137.         }
  138.         CNL_Prefs.setDicsArrayPref(dicservices);
  139.         Observers.notify("cnl_dicservicesPreferencesChanged");
  140.     },
  141.     validate: function(){        
  142.         var allDictDisabled = true;
  143.         for(var i=1; i<this.rows.childElementCount; i++)
  144.             if(this.rows.childNodes[i].childNodes[0].checked){
  145.                 allDictDisabled = false;
  146.                 break;
  147.             }
  148.         if(allDictDisabled){
  149.             alert("Can't disable all dictionary sources!");
  150.             return false;
  151.         }
  152.         
  153.         return true;
  154.     },
  155.     /**
  156.      * see: https://developer.mozilla.org/En/Code_snippets/Tabbed_browser
  157.      */
  158.     help: function(){
  159.         var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  160.                            .getService(Components.interfaces.nsIWindowMediator);
  161.         var mainWindow = wm.getMostRecentWindow("navigator:browser");
  162.  
  163.         // Add tab, then make active
  164.         mainWindow.gBrowser.selectedTab = mainWindow.gBrowser.addTab(
  165.                 "http://code.google.com/p/clicknlearn/wiki/UserGuide#Add_dictionary_sources");
  166.         mainWindow.focus();
  167.     }
  168. }
  169.  
  170. window.addEventListener("load", function(){CNL_Options.eventLoadImpl();}, false);
  171.